package com.saicent.note.core;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.Element;

import com.saicent.support.io.ConfigurationReader;

public class ConfigurationBuilder {
	
	private Document document = null;
	private String workDirectory = "/";
	
	public ConfigurationBuilder() {}
	
	public ConfigurationBuilder(String workDirectory) {
		this.workDirectory = workDirectory;
	}
	
	public Configuration build(String filename) {
		document = ConfigurationReader.readXML(filename);
		Configuration configuration = parse(document);
		configuration.setWorkDirectory(workDirectory);
		return configuration;
	}
	
	public Configuration build() {
		return build("notes.xml");
	}
	
	private Configuration parse(Document document) {
		Configuration configuration = new Configuration();
		List<Article> articles = articleElements(document.getRootElement());
		Element element = document.getRootElement().element("auto-scanning");
		if (element != null) {
			configuration.setScanningLocation(element.attributeValue("location"));
		}
		configuration.setArticles(articles);
		return configuration;
	}
	
	private List<Article> articleElements(Element element) {
		//TODO:
		return new ArrayList<>();
//		List<Article> noteMappings = new ArrayList<>();
//		List<Element> elements = element.elements("note-mapping");
//		for (Element nodeMappingElement : elements) {
//			String title = nodeMappingElement.elementText("title");
//			String location = nodeMappingElement.elementText("location");
//			String categories = nodeMappingElement.elementText("categories");
//			if (StringUtils.isBlank(location)) {
//				continue;
//			}
//			Article noteMapping = new Article(title, location, categories);
//			noteMappings.add(noteMapping);
//		}
//		return noteMappings;
	}
	
}
